/**********************工具类,由于函数不多,所以没有分工具种类划分****************************/


/**
 * 获取URL地址的查询参数
 *
 * @date 2017-3-2
 * @author 魏明欣
 *
 * @returns
 */
function getRequest() {

    var url = decodeURI(location.href);

    if (url.indexOf("?") === -1) {
        return;
    }

    var str = url.substr(url.indexOf("?") + 1);
    var strs = str.split("&");

    var theRequest = {};
    for (var i = 0; i < strs.length; i++) {
        theRequest[strs[i].split("=")[0]] = unescape(strs[i].split("=")[1]);
    }

    return theRequest;
}

/**
 * 返回到上一个页面
 *
 * @date 2017-3-2
 * @author 魏明欣
 *
 * @returns
 */
function goBackPage() {// 参考history.js

    if (!history) {
        return


    }

    history.back();
}

/**
 * 重新设置父页面的高度
 *
 * @date 2017-3-19
 * @author 魏明欣
 *
 */
function resizeParentIframe() {
    var maineIframe = $("iframe", window.parent.document);
    maineIframe.height(0);
    // maineIframe.width(0);

    var thisPageHight = $(window.document).height();
    var thisPageWidth = $(window.document).width();

    maineIframe.height(thisPageHight + 40);
    // maineIframe.width(thisPageWidth-20);

    //console.info(thisPageHight);
}

/**
 * 获取工程根目录
 *
 * @returns {String}
 */
function getRootPath() {
    var strFullPath = window.document.location.href;
    var strPath = window.document.location.pathname;

    var pos = strFullPath.indexOf(strPath);
    var prePath = strFullPath.substring(0, pos);
    var postPath = strPath.substring(0, strPath.substr(1).indexOf('/') + 1);
    return (prePath + postPath);
};

/**
 * 表单转json
 *
 * @param $form
 *            表单对象
 * @returns {{}}
 */
function form2Json($form) {
    var obj = {};
    if ($form.length == 1) {
        var arr = $form.serializeArray();
        $.each(arr, function (k, v) {
            obj[v.name] = v.value;
        });
    }
    return obj;
};
/**
 * 是否是数字
 *
 * @param value
 */
function isNumber(value) {
    var pattern = /^[0-9]*$/;
    if (pattern.exec(value) == null || value === "") {
        return false;
    } else {
        return true;
    }
}
/**
 * 转boolean
 *
 * @param obj
 * @returns {boolean}
 */
function obj2bool(obj) {
    var bool = false;
    if (!!obj) {
        if (typeof obj === "string") {
            if (obj.toLowerCase() === "false" || obj.toLowerCase() === "no")
                bool = false;
            else if (obj.toLowerCase() === "true"
                || obj.toLowerCase() === "yes")
                bool = true;
        } else {
            bool = true;
        }
    }
    return bool;
};
/**
 * 删除数组元素
 *
 * @param dx
 *            索引
 * @param arr
 *            目标数组
 * @returns {boolean}
 */
function removeArrayItem(dx, arr) {
    if (isNaN(dx) || dx > arr.length) {
        return false;
    }
    for (var i = 0, n = 0; i < arr.length; i++) {
        if (arr[i] != arr[dx]) {
            arr[n++] = arr[i];
        }
    }
    arr.length -= 1;
};
/**
 * 首字母转大写
 *
 * @param str
 * @returns {string}
 */
function firstUpperCase(str) {
    return str.toLowerCase().replace(/\b[a-z]/g, function (s) {
        return s.toUpperCase();
    });
}
/**
 * 获取当前的年份、季度、月份、日期、该季度的第一月份
 */
function getCurrentYearQuarter() {
	var nowDate=new Date();
    var year = parseInt(nowDate.getFullYear());// 当前年份
    var month = parseInt(nowDate.getMonth());
    var  date= parseInt(nowDate.getDate());

    var quarter = 1;
    var firstMonth=3;
    if (month >= 3 && month <= 5) {
    } else if (month >= 6 && month <= 8) {
    	firstMonth=6;
        quarter = 2;
    } else if (month >= 9 && month <= 11) {
    	firstMonth=9
        quarter = 3;
    } else {
        quarter = 4;
        if (month >= 1 && month <= 2)
        {
        	firstMonth=1;
            year = year - 1;
        }
    }
    return {
        year: year,
        quarter: quarter,
        month:month,
        date:date,
        firstMonth:firstMonth
    };
}
/**
 * 格式化日期
 *
 * @param long
 *            日期long
 * @param pattern
 *            格式
 * @returns {*}
 */
function formatDate(long, pattern) {
    var date = new Date(long);
    if (pattern == undefined) {
        pattern = "yyyy-MM-dd hh:mm:ss";
    }
    var o = {
        "M+": date.getMonth() + 1,
        "d+": date.getDate(),
        "h+": date.getHours(),
        "m+": date.getMinutes(),
        "s+": date.getSeconds(),
        "q+": Math.floor((date.getMonth() + 3) / 3),
        "S": date.getMilliseconds()
    };
    if (/(y+)/.test(pattern)) {
        pattern = pattern.replace(RegExp.$1, (date.getFullYear() + "").substr(4 - RegExp.$1.length));
    }
    for (var k in o) {
        if (new RegExp("(" + k + ")").test(pattern)) {
            pattern = pattern.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
        }
    }
    return pattern;
}
/**
 * 字符串去空格
 *
 * @param str
 * @returns {string|XML|void}
 */
function trim(str) {
    return str.replace(/^\s+|\s+$/gm, '');
}
/**
 * 获取cookie值
 *
 * @param name
 *            名称
 * @returns {*}
 */
function getCookie(name) {
    // debugger
    var cookieList = document.cookie.split(";");
    for (var i = 0; i < cookieList.length; i++) {
        var cookie = cookieList[i];
        var pair = cookie.split("=");
        if (trim(pair[0]) === name) {
            return pair[1];
        }

    }
    return "";
}
/**
 * 滚动到
 *
 * @param $container
 *            容器对象
 * @param $target
 *            目标对象
 */
function scrollTo($container, $target) {
    if(!!$target){
        var offset = $target.offset();
        if(!!offset){
            $container.animate({
                scrollTop: offset.top - $container.offset().top + $container.scrollTop()
            })
        }

    }
}
/**
 * 获取combotree复选框获取选中的节点，attributes的isUsable为true的id
 */
$.fn.getCheckUsableTrue = function () {
    var values = "";
    var t = this.combotree('tree');	// 获取树对象
    var nodes = t.tree('getChecked');		// 获取选择的节点
    for (var i = 0; i < nodes.length; i++) {
        if (nodes[i].attributes.isUsable == true) {
            if (values == "")
                values = nodes[i].id;
            else
                values = values + "," + nodes[i].id;
        }
    }
    return values;
}
/**
 * 字符串若为undefined或null则返回''
 */
function emptytreat(str){
	if(str==undefined||str==null)
		return '';
	return str;
}

/**
 * 获取窗口对象
 * @param id
 * @returns {*|jQuery}
 */
function getWindowObject(id){
    var html = $(id).contents().find("body").html();
    return $(html).appendTo("body");;
}


/**
 * 验证按钮是否显示 (流程里按钮的显示)
 *
 * @param params   参数数据
 * @param func     回调
 */
function buttonIsShow(params, func) {
    var res;
    $.ajax({
        type: 'post',
        url: getRootPath() + "/audit/isusablebutton",
        dataType: 'json',
        async: false,// 同步
        data: params,
        success: function (data) {
            res = data.value;
        }
    });
    return res;
}

function hiddenDatagridToolbar($grid){
	 var panel = $grid.datagrid("getPanel");
	 panel.find("div.datagrid-toolbar").remove();
}
//获取窗口对象
function getWindowObject(id){
    var html = $("#win_iframe").contents().find("body").html();
    
    return $(html).appendTo("body");
}
